package win.smartown.aqst.demo.net;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import win.smartown.aqst.demo.entity.Response;

public abstract class RequestListener<Result> implements Observer<Response<Result>> {

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(Response<Result> response) {
        if (response.getErrorCode() == 0) {
            onSuccess(response.getResult());
        } else {
            onError(new Exception(response.getReason()));
        }
    }

    @Override
    public void onError(Throwable e) {
        onFail(e);
        onFinish();
    }

    @Override
    public void onComplete() {
        onFinish();
    }

    public abstract void onSuccess(Result result);

    public abstract void onFail(Throwable e);

    public abstract void onFinish();

}